package com.dx.mes.service.uas.service;

import static com.dx.mes.common.bean.def.GenericDef.ACTION_FLG_ADD;
import static com.dx.mes.common.bean.def.GenericDef.ACTION_FLG_BCCHECK_BTN;
import static com.dx.mes.common.bean.def.GenericDef.ACTION_FLG_BCCHECK_LOGIN;
import static com.dx.mes.common.bean.def.GenericDef.ACTION_FLG_BCCHECK_PAGE;
import static com.dx.mes.common.bean.def.GenericDef.ACTION_FLG_DELETE;
import static com.dx.mes.common.bean.def.GenericDef.ACTION_FLG_LIST_WHOLE;
import static com.dx.mes.common.bean.def.GenericDef.ACTION_FLG_QUERY;
import static com.dx.mes.common.bean.def.GenericDef.ACTION_FLG_UPDATE;
import static com.dx.mes.common.bean.def.GenericDef.EVT_UAS_ADD;
import static com.dx.mes.common.bean.def.GenericDef.EVT_UAS_UPDATE;
import static com.dx.mes.common.bean.def.GenericDef.E_ADD_EXIST;
import static com.dx.mes.common.bean.def.GenericDef.E_BIS_UAS_GRP;
import static com.dx.mes.common.bean.def.GenericDef.E_BIS_USER;
import static com.dx.mes.common.bean.def.GenericDef.E_FAPUASGRP_DATA_EMPTY;
import static com.dx.mes.common.bean.def.GenericDef.E_FAPUASGRP_GRP_BIND_FUNC;
import static com.dx.mes.common.bean.def.GenericDef.E_FAPUASGRP_GRP_EMPRT;
import static com.dx.mes.common.bean.def.GenericDef.E_READ_NOT_FOUND;
import static com.dx.mes.common.bean.def.GenericDef.GROUP_ADMIN_FLG;
import static com.dx.mes.common.bean.def.GenericDef.PROP_CATE_DEST;
import static com.dx.mes.common.bean.def.GenericDef.PROP_CATE_OPE;
import static com.dx.mes.common.bean.def.GenericDef.PROP_CATE_TOOL;
import static com.dx.mes.common.bean.def.GenericDef.RETURN_CODE_OK;
import static com.dx.mes.common.bean.def.GenericDef.RETURN_CODE_SET_ERROR;
import static com.dx.mes.common.bean.def.GenericDef.RETURN_CODE_UNKNOWN;
import static com.dx.mes.common.bean.def.GenericDef.RETURN_MESG_OK;
import static com.dx.mes.common.bean.def.GenericDef.TRX_OUT;
import static com.dx.mes.common.bean.def.GenericDef.T_FAPUASGRP;
import static com.dx.mes.common.bean.def.GenericDef._ERROR;
import static com.dx.mes.common.bean.def.GenericDef._NORMAL;
import static java.util.stream.Collectors.toList;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.dx.common.table.ppt.Bis_uas_fnc_code;
import com.dx.common.table.ppt.Bis_uas_fnc_grp;
import com.dx.common.table.ppt.Bis_uas_fnc_grpId;
import com.dx.common.table.ppt.Bis_uas_grp;
import com.dx.common.table.ppt.Bis_uas_special;
import com.dx.common.table.ppt.Bis_uas_specialId;
import com.dx.common.table.ppt.Bis_uas_usr_grp;
import com.dx.common.table.ppt.Bis_uas_usr_grpId;
import com.dx.common.table.ppt.Bis_user;
import com.dx.mes.common.dao.IBisUasFncCodeDao;
import com.dx.mes.common.dao.IBisUasFncGrpDao;
import com.dx.mes.common.dao.IBisUasGrpDao;
import com.dx.mes.common.dao.IBisUasSpecialDao;
import com.dx.mes.common.dao.IBisUasUsrGrpDao;
import com.dx.mes.common.dao.IBisUserDao;
import com.dx.mes.common.transcoder.util.AppContext;
import com.dx.mes.common.transcoder.util.JacksonUtil;
import com.dx.mes.common.transcoder.util.LogUtils;
import com.dx.mes.common.transcoder.util.Util;
import com.dx.mes.facade.uas.service.IBisUasFunGrpService;
import com.dx.mes.facade.uas.service.IFapuasgrpService;
import com.dx.mes.facade.uas.tx.Fapuasgrp.FapuasgrpI;
import com.dx.mes.facade.uas.tx.Fapuasgrp.FapuasgrpIA;
import com.dx.mes.facade.uas.tx.Fapuasgrp.FapuasgrpIB;
import com.dx.mes.facade.uas.tx.Fapuasgrp.FapuasgrpIC;
import com.dx.mes.facade.uas.tx.Fapuasgrp.FapuasgrpO;
import com.dx.mes.facade.uas.tx.Fapuasgrp.FapuasgrpOA;
import com.dx.mes.facade.uas.tx.Fapuasgrp.FapuasgrpOB;

@Service("fapuasgrpService")
@SuppressWarnings({"unchecked"})
public class FapuasgrpService implements IFapuasgrpService{
private LogUtils logUtils;
private static String TX_NAME = T_FAPUASGRP;

@Autowired
private IBisUserDao bisUserDao;
@Autowired
private IBisUasGrpDao bisUasGrpDao;
@Autowired 
private IBisUasUsrGrpDao bisUasUsrGrpDao;
@Autowired
private IBisUasFncCodeDao bisUasFncCodeDao;
@Autowired
private IBisUasFncGrpDao bisUasFncGrpDao;
@Autowired
private IBisUasSpecialDao bisUasSpecialDao;
@Autowired
private IBisUasFunGrpService bisUasFunGrpService;

public String subMainProc(String evt_no, String strInMsg) {
	AppContext.clear();
	AppContext.setCurrServiceName(FapuasgrpService.class.getSimpleName());
	AppContext.setCurrEventNumber(evt_no);
	logUtils = new LogUtils(FapuasgrpService.class);
	logUtils.info("[InTrx:" + strInMsg + "]");
	
	Session session = bisUserDao.getCrSession();
	long rtnCode = _NORMAL;
	String outMsg = null;

	FapuasgrpI inTrx = new FapuasgrpI();
	FapuasgrpO outTrx = new FapuasgrpO();
	outTrx.setTrx_id(TX_NAME);
	outTrx.setRtn_code(String.valueOf(RETURN_CODE_SET_ERROR));
	outTrx.setType_id(TRX_OUT);


	try {
		inTrx = JacksonUtil.fromJson(strInMsg, FapuasgrpI.class);

		rtnCode = subMainProc2(inTrx, outTrx);
		if (rtnCode != _NORMAL) {
			TransactionAspectSupport.currentTransactionStatus()
					.setRollbackOnly();
			outTrx.setRtn_code(String.valueOf(rtnCode));
		} else {
			outTrx.setRtn_code(String.valueOf(RETURN_CODE_OK));
			outTrx.setRtn_mesg(RETURN_MESG_OK);
			session.flush();
		}
	} catch (Exception ex) {
		TransactionAspectSupport.currentTransactionStatus()
				.setRollbackOnly();
		outTrx.setRtn_mesg(ex.toString());
		outTrx.setRtn_code(RETURN_CODE_UNKNOWN);
		logUtils.error(Util.stackTraceToString(ex));
	} finally {
		outMsg = JacksonUtil.toJSONStr(outTrx);
		logUtils.info("[OutTrx:" + outMsg + "]");
	}
	return outMsg;
}

private long subMainProc2(FapuasgrpI inTrx, FapuasgrpO outTrx) {
	long lRc = _NORMAL;
	char actionType = inTrx.getAction_flg().toCharArray()[0];
	switch (actionType) {
	case ACTION_FLG_QUERY:
        lRc = queryByCondition(inTrx, outTrx);
        if (lRc != _NORMAL) {
			return lRc;
		}
        break;
	case ACTION_FLG_DELETE:
		lRc = deleteFnc(inTrx, outTrx);
		 if (lRc != _NORMAL) {
				return lRc;
			}
        break; 
	case ACTION_FLG_ADD:
		lRc = addFnc(inTrx, outTrx);
		 if (lRc != _NORMAL) {
				return lRc;
			}
        break;
	case ACTION_FLG_UPDATE:
		lRc = updateFnc(inTrx, outTrx);
		 if (lRc != _NORMAL) {
				return lRc;
			}
	case ACTION_FLG_LIST_WHOLE:
		lRc = listGrpWholeFnc(inTrx,outTrx);
		 if (lRc != _NORMAL) {
				return lRc;
			}
        break;
	case 'G':
		lRc = queryGrpFnc(inTrx,outTrx);
		 if (lRc != _NORMAL) {
				return lRc;
			}
        break; 
	case 'B':
		lRc = queryUserGrpFnc(inTrx,outTrx);
		 if (lRc != _NORMAL) {
				return lRc;
			}
        break;
	case 'R':
		lRc = regUsrGrpFnc(inTrx,outTrx);
		 if (lRc != _NORMAL) {
				return lRc;
			}
        break;
	case 'F':
		lRc = listFuncCodeWholeFnc(inTrx,outTrx);
		 if (lRc != _NORMAL) {
				return lRc;
			}
        break;
	case 'C':
		lRc = queryGrpFuncCodeFnc(inTrx,outTrx);
		 if (lRc != _NORMAL) {
				return lRc;
			}
        break;
	case 'E':
		lRc = regGrpFuncFnc(inTrx,outTrx);
		 if (lRc != _NORMAL) {
				return lRc;
			}
        break;
	case 'H':
		lRc = querySpecialFnc(inTrx,outTrx);
		 if (lRc != _NORMAL) {
				return lRc;
			}
        break;
	case 'I':
		lRc = regUasSpecialFnc(inTrx,outTrx);
		 if (lRc != _NORMAL) {
				return lRc;
			}
        break;
	case 'J':
		lRc = regUasGrpFnc(inTrx,outTrx);
		if (lRc != _NORMAL){
			   return lRc;
		}
		break;
	case 'P':
		lRc = updateUasGrpFnc(inTrx,outTrx);
		if (lRc != _NORMAL){
			return lRc;
		}
		break;
	case ACTION_FLG_BCCHECK_LOGIN:
			lRc = bcCheckLoginFnc(inTrx,outTrx);
			if(lRc != _NORMAL){
				return lRc;
			}
		break;
	case ACTION_FLG_BCCHECK_PAGE:
			lRc = bcCheckPageFnc(inTrx,outTrx);
			if(lRc != _NORMAL){
				return lRc;
			}
			break;
	case ACTION_FLG_BCCHECK_BTN:
			lRc = bcCheckBtnFnc(inTrx,outTrx);
			if(lRc != _NORMAL){
				return lRc;
			}
			break;
		case 'T':
			lRc = deleteGroup(inTrx,outTrx);
			break;
	default:
         errActionFnc(outTrx);
	}
	return lRc;
}
	private  boolean  authCheckFnc(String usr_id,String fnc_code){
		boolean hasAuth = false;
		List<Bis_uas_fnc_grp> permission = bisUasFunGrpService.getPermission(usr_id);
		permission = permission == null ? new ArrayList<>() : permission;
		List<String> permissionList = permission.stream().map(p -> p.getId().getFunc_code_fk()).collect(toList());
		//List<String> perList = permissionList.stream().map(per -> per.substring(0, per.lastIndexOf("_"))).collect(toList());
		hasAuth =  permissionList.contains(fnc_code);
		return hasAuth;
	}

	private long bcCheckBtnFnc(FapuasgrpI inTrx, FapuasgrpO outTrx) {
		long lRc = _NORMAL;
		List<FapuasgrpIC> iaryC = inTrx.getIaryC();
		String usr_id = iaryC.get(0).getUsr_id();
		String fnc_code = iaryC.get(0).getFnc_code();
		boolean bc_flg = this.authCheckFnc(usr_id,fnc_code);
		if(!bc_flg){
			return _ERROR;
		}else {
			return lRc;
		}
	}

	private long bcCheckPageFnc(FapuasgrpI inTrx, FapuasgrpO outTrx) {
		long lRc = _NORMAL;
		List<FapuasgrpIC> iaryC = inTrx.getIaryC();
		String usr_id = iaryC.get(0).getUsr_id();
		String fnc_name = iaryC.get(0).getFnc_name();
		String hql = "From Bis_uas_fnc_code where func_name = '"+fnc_name+"'";
		List<Bis_uas_fnc_code> codes = bisUasFncCodeDao.find(hql);
		if(codes.isEmpty()){
			outTrx.setRtn_mesg("没有对应的func_code");
			return _ERROR;
		}
		String fnc_code = codes.get(0).getFunc_code();
		boolean bc_flg = this.authCheckFnc(usr_id,fnc_code);
		if(!bc_flg){
			outTrx.setRtn_mesg("没有页面权限");
			return _ERROR;
		}else {
			outTrx.setBc_flg(bc_flg);
			return lRc;
		}
	}

	private long bcCheckLoginFnc(FapuasgrpI inTrx, FapuasgrpO outTrx) {
		long lRc = _NORMAL;
		List<FapuasgrpIC> iaryC = inTrx.getIaryC();
		String usr_id = iaryC.get(0).getUsr_id();
		Bis_user bis_user = bisUserDao.get(usr_id);
		if(null == bis_user){
			outTrx.setRtn_mesg("查询不到用户");
			return  _ERROR;
		}
		String usr_key = iaryC.get(0).getUsr_key();
		boolean flg = bisUasFunGrpService.getUserByUsrIdAndPwd(usr_id,usr_key);
		if(flg){
			return  lRc;
		}else{
			outTrx.setRtn_mesg("没有登陆权限");
			return  _ERROR;
		}
	}


	private long regUasGrpFnc(FapuasgrpI inTrx, FapuasgrpO outTrx) {
		long lRc = _NORMAL;
		Timestamp current_timestamp = Util.getCurrentTimestamp();
		FapuasgrpIB iaryB = inTrx.getIaryB().get(0);
		String group_id = inTrx.getGroup_id();
		String group_name = iaryB.getGroup_name();
		String admin_flg = iaryB.getAdmin_flg();
		String access_flg = iaryB.getAccess_flg();
		String system_id = iaryB.getSystem_id();
		Bis_uas_grp bis_uas_grp = bisUasGrpDao.get(group_id);
		if(StringUtils.isBlank(group_id)){
			outTrx.setRtn_mesg("群组代码[" + group_id + "]为空，请确认！");
			return E_FAPUASGRP_DATA_EMPTY;
		}
		if(StringUtils.isBlank(group_name)){
			outTrx.setRtn_mesg("群组名称[" + group_name + "]为空，请确认！");
			return E_FAPUASGRP_DATA_EMPTY;
		}
		if (bis_uas_grp != null){
			outTrx.setRtn_mesg("代码为[" + inTrx.getGroup_id() + "]的功能组已经存在!请确认！");
			return E_BIS_UAS_GRP +E_ADD_EXIST;
		}

		Bis_uas_grp  bisUasGrp = new Bis_uas_grp();
		bisUasGrp.setGroup_id(group_id);
		bisUasGrp.setGroup_name(group_name);
		bisUasGrp.setAdmin_flg(admin_flg);
		bisUasGrp.setAccess_id(access_flg);
		bisUasGrp.setSystem_id(system_id);
		bisUasGrp.setEvt_timestamp(current_timestamp);
		bisUasGrp.setEvt_usr(inTrx.getEvt_usr());
		bisUasGrpDao.save(bisUasGrp);
		return lRc;
	}

	private long regUasSpecialFnc(FapuasgrpI inTrx, FapuasgrpO outTrx) {
	long lRc = _NORMAL;
	Timestamp current_timestamp = Util.getCurrentTimestamp();
	List<FapuasgrpIB> iaryBList = inTrx.getIaryB();
	String usr_id_fk = iaryBList.get(0).getUsr_id_fk();
	String hql = "FROM Bis_uas_special where usr_id_fk = '"+usr_id_fk+"' and system_id ='OPI'";
	List<Bis_uas_special> bis_uas_specials= bisUasSpecialDao.find(hql);
	for (Bis_uas_special bis_uas_special : bis_uas_specials) {
		bis_uas_special.setEvt_usr(inTrx.getEvt_usr());
		bis_uas_special.setEvt_timestamp(current_timestamp);
		bisUasSpecialDao.delete(bis_uas_special);
	}
	for (FapuasgrpIB iaryB : iaryBList) {
		String prop_id = iaryB.getProp_id();
		String prop_cate = iaryB.getProp_cate();
		Bis_uas_specialId id = new Bis_uas_specialId();
		id.setUsr_id_fk(usr_id_fk);
		id.setProp_id(prop_id);
		Bis_uas_special bisUasSpecial =new Bis_uas_special();
		bisUasSpecial.setId(id);
		if (prop_cate.equals("OPE")) {
			bisUasSpecial.setProp_cate(PROP_CATE_OPE);
		}else if (prop_cate.equals("TOOL")) {
			bisUasSpecial.setProp_cate(PROP_CATE_TOOL);
		}else if (prop_cate.equals("DEST")) {
			bisUasSpecial.setProp_cate(PROP_CATE_DEST);
		}
		bisUasSpecial.setEvt_usr(inTrx.getEvt_usr());
		bisUasSpecial.setEvt_timestamp(current_timestamp);
		bisUasSpecial.setSystem_id("OPI");
		bisUasSpecialDao.save(bisUasSpecial);
	}
	return lRc;
}

private long querySpecialFnc(FapuasgrpI inTrx, FapuasgrpO outTrx) {
	long lRc = _NORMAL;	
	FapuasgrpIB iaryB = inTrx.getIaryB().get(0);
	String usr_id_fk = iaryB.getUsr_id_fk();
	String hql = "FROM Bis_uas_special where 1=1 and system_id ='OPI'";
	if (!Util.isSpaceCheck(usr_id_fk)) {
		hql += "and usr_id_fk = '"+usr_id_fk+"'";
	}
	List<Bis_uas_special> bis_uas_specials= bisUasSpecialDao.find(hql);
	List<FapuasgrpOB> oaryBList = new ArrayList<FapuasgrpOB>();
	for (Bis_uas_special bis_uas_special : bis_uas_specials) {
		FapuasgrpOB oaryB = new FapuasgrpOB();
		oaryB.setProp_id(bis_uas_special.getId().getProp_id());
		oaryBList.add(oaryB);
	}
	outTrx.setOaryB(oaryBList);
	outTrx.setTbl_cnt(oaryBList.size());
	return lRc;
}

private long regGrpFuncFnc(FapuasgrpI inTrx, FapuasgrpO outTrx) {
	long lRc = _NORMAL;
	Timestamp current_timestamp = Util.getCurrentTimestamp();
	List<FapuasgrpIB> iaryBList = inTrx.getIaryB();
	String group_id = inTrx.getGroup_id();
	String hql = "FROM Bis_uas_fnc_grp where group_id_fk = '"+group_id+"'";
	List<Bis_uas_fnc_grp> bis_uas_fnc_grps= bisUasFncGrpDao.find(hql);
	if(bis_uas_fnc_grps == null && bis_uas_fnc_grps.size() == 0  && iaryBList.size()== 0 ){
		outTrx.setRtn_mesg("该功能组之前未绑定功能代码，请选择功能代码！");
		return E_FAPUASGRP_GRP_EMPRT;
	}
	for (Bis_uas_fnc_grp bis_uas_fnc_grp : bis_uas_fnc_grps) {
		bis_uas_fnc_grp.setEvt_usr(inTrx.getEvt_usr());
		bis_uas_fnc_grp.setEvt_timestamp(current_timestamp);
		bisUasFncGrpDao.delete(bis_uas_fnc_grp);
	}
	for (FapuasgrpIB iaryB : iaryBList) {
		String func_code = iaryB.getFunc_code();
		Bis_uas_fnc_grpId id = new Bis_uas_fnc_grpId();
		id.setGroup_id_fk(group_id);
		id.setFunc_code_fk(func_code);
		Bis_uas_fnc_grp bisUasFncGrp =new Bis_uas_fnc_grp();
		bisUasFncGrp.setId(id);
		bisUasFncGrp.setEvt_usr(inTrx.getEvt_usr());
		bisUasFncGrp.setSystem_id("OPI");
		bisUasFncGrp.setEvt_timestamp(current_timestamp);
		bisUasFncGrpDao.save(bisUasFncGrp);
	}
	return lRc;
}

private long queryGrpFuncCodeFnc(FapuasgrpI inTrx, FapuasgrpO outTrx) {
	long lRc = _NORMAL;
	FapuasgrpIB iaryB = inTrx.getIaryB().get(0);
	String group_id_fk = iaryB.getGroup_id();
	String hql = "FROM Bis_uas_fnc_grp where 1=1 and system_id ='OPI'";
	if (!Util.isSpaceCheck(group_id_fk)) {
		hql += "and group_id_fk = '"+group_id_fk+"'";
	}
	List<Bis_uas_fnc_grp> bis_uas_fnc_grps = bisUasFncGrpDao.find(hql);
	List<FapuasgrpOB> oaryBList = new ArrayList<FapuasgrpOB>();
	for (Bis_uas_fnc_grp bis_uas_fnc_grp : bis_uas_fnc_grps) {
		FapuasgrpOB oaryB = new FapuasgrpOB();
		oaryB.setFunc_code(bis_uas_fnc_grp.getId().getFunc_code_fk());
		oaryBList.add(oaryB);
	}
	outTrx.setOaryB(oaryBList);
	outTrx.setTbl_cnt(oaryBList.size());
	return lRc;
}

private long listFuncCodeWholeFnc(FapuasgrpI inTrx, FapuasgrpO outTrx) {
	long lRc = _NORMAL;
	
	List<FapuasgrpOB> oaryBList = new ArrayList<FapuasgrpOB>();
	String hql = "FROM Bis_uas_fnc_code where 1=1 and system_id ='OPI' ";
	List<Bis_uas_fnc_code> Bis_uas_fnc_codes = bisUasFncCodeDao.find(hql);
	for (Bis_uas_fnc_code bis_uas_fnc_code : Bis_uas_fnc_codes) {
		FapuasgrpOB oaryB = new FapuasgrpOB();
		oaryB.setFunc_code(bis_uas_fnc_code.getFunc_code());
		oaryB.setFunc_name(bis_uas_fnc_code.getFunc_name());
		oaryB.setSystem_id(bis_uas_fnc_code.getSystem_id());
		oaryBList.add(oaryB);
	}
	outTrx.setOaryB(oaryBList);
	outTrx.setTbl_cnt(oaryBList.size());
	
	return lRc;
}

private long queryGrpFnc(FapuasgrpI inTrx, FapuasgrpO outTrx) {
	long lRc = _NORMAL;
	
	FapuasgrpIB iaryB = inTrx.getIaryB().get(0);
	List<FapuasgrpOB> oaryBList = new ArrayList<FapuasgrpOB>();
	String hql = "FROM Bis_uas_grp where 1=1 and system_id ='OPI'";
	if (iaryB.getGroup_id() != null) {
		hql += "and group_id ='"+iaryB.getGroup_id()+"'";
	}
	List<Bis_uas_grp> bis_uas_grps = bisUasGrpDao.find(hql);
	for (Bis_uas_grp bis_uas_grp : bis_uas_grps) {
		FapuasgrpOB oaryB = new FapuasgrpOB();
		oaryB.setGroup_id(bis_uas_grp.getGroup_id());
		oaryB.setGroup_name(bis_uas_grp.getGroup_name());
		oaryB.setAdmin_flg(bis_uas_grp.getAdmin_flg());
		oaryB.setSystem_id(bis_uas_grp.getSystem_id());
		oaryBList.add(oaryB);
	}
	outTrx.setOaryB(oaryBList);
	outTrx.setTbl_cnt(oaryBList.size());
	return lRc;
}

private long regUsrGrpFnc(FapuasgrpI inTrx, FapuasgrpO outTrx) {
	long lRc = _NORMAL;
	Timestamp current_timestamp = Util.getCurrentTimestamp();
	List<FapuasgrpIB> iaryBList = inTrx.getIaryB();
	String usr_id_fk = inTrx.getUsr_id_fk();
	String hql = "FROM Bis_uas_usr_grp where usr_id_fk = '"+usr_id_fk+"'";
	List<Bis_uas_usr_grp> bis_uas_usr_grps= bisUasUsrGrpDao.find(hql);
	if(bis_uas_usr_grps == null && bis_uas_usr_grps.size() == 0  && iaryBList.size()== 0 ){
		outTrx.setRtn_mesg("该用户之前未绑定功能组，请选择功能组！");
		return E_FAPUASGRP_GRP_EMPRT;
	}
	for (Bis_uas_usr_grp bis_uas_usr_grp : bis_uas_usr_grps) {
		bis_uas_usr_grp.setEvt_usr(inTrx.getEvt_usr());
		bis_uas_usr_grp.setEvt_timestamp(current_timestamp);
		bisUasUsrGrpDao.delete(bis_uas_usr_grp);
	}
	for (FapuasgrpIB iaryB : iaryBList) {
		String group_id_fk = iaryB.getGroup_id();
		Bis_uas_usr_grpId id = new Bis_uas_usr_grpId();
		id.setGroup_id_fk(group_id_fk);
		id.setUsr_id_fk(usr_id_fk);
		Bis_uas_usr_grp bisUasUsrGrp =new Bis_uas_usr_grp();
		bisUasUsrGrp.setId(id);
		bisUasUsrGrp.setSystem_id("OPI");
		bisUasUsrGrp.setEvt_usr(inTrx.getEvt_usr());
		bisUasUsrGrp.setEvt_timestamp(current_timestamp);
		bisUasUsrGrpDao.save(bisUasUsrGrp);
	}
	return lRc;
}

private long queryUserGrpFnc(FapuasgrpI inTrx, FapuasgrpO outTrx) {
	long lRc = _NORMAL;	
	FapuasgrpIB iaryB = inTrx.getIaryB().get(0);
	String usr_id_fk = iaryB.getUsr_id_fk();
	String hql = "FROM Bis_uas_usr_grp where 1=1 and system_id ='OPI'";
	if (!Util.isSpaceCheck(usr_id_fk)) {
		hql += "and usr_id_fk = '"+usr_id_fk+"'";
	}
	List<Bis_uas_usr_grp> bis_uas_usr_grps= bisUasUsrGrpDao.find(hql);
	List<FapuasgrpOB> oaryBList = new ArrayList<FapuasgrpOB>();
	for (Bis_uas_usr_grp bis_uas_usr_grp : bis_uas_usr_grps) {
		FapuasgrpOB oaryB = new FapuasgrpOB();
		oaryB.setGroup_id(bis_uas_usr_grp.getId().getGroup_id_fk());
		oaryBList.add(oaryB);
	}
	outTrx.setOaryB(oaryBList);
	outTrx.setTbl_cnt(oaryBList.size());
	return lRc;
}

private long listGrpWholeFnc(FapuasgrpI inTrx, FapuasgrpO outTrx) {
	long lRc = _NORMAL;
	
	List<FapuasgrpOB> oaryBList = new ArrayList<FapuasgrpOB>();
	String hql = "FROM Bis_uas_grp where 1=1 and system_id ='OPI'";
	List<Bis_uas_grp> bis_uas_grps = bisUasGrpDao.find(hql);
	for (Bis_uas_grp bis_uas_grp : bis_uas_grps) {
		FapuasgrpOB oaryB = new FapuasgrpOB();
		oaryB.setGroup_id(bis_uas_grp.getGroup_id());
		oaryB.setGroup_name(bis_uas_grp.getGroup_name());
		oaryB.setAdmin_flg(bis_uas_grp.getAdmin_flg());
		oaryB.setSystem_id(bis_uas_grp.getSystem_id());
		oaryBList.add(oaryB);
	}
	outTrx.setOaryB(oaryBList);
	outTrx.setTbl_cnt(oaryBList.size());
	return lRc;
}

private long updateFnc(FapuasgrpI inTrx, FapuasgrpO outTrx) {
	long lRc = _NORMAL;
	FapuasgrpIA iary = inTrx.getIary().get(0);
	Timestamp current_timestamp = Util.getCurrentTimestamp();
	
	String usr_id = iary.getUsr_id();
	Bis_user bis_user = bisUserDao.get(usr_id);
	if (bis_user == null) {
		outTrx.setRtn_mesg("not Found Record[BIS_USER]");
		return E_BIS_USER +E_READ_NOT_FOUND; 
	}
	if (iary.getUsr_name() != null) {
		bis_user.setUsr_name(iary.getUsr_name());
	}
	if (iary.getUsr_e_name() != null) {
		bis_user.setUsr_e_name(iary.getUsr_e_name());
	}
	if (iary.getUsr_typ() != null) {
		bis_user.setUsr_typ(iary.getUsr_typ());
	}
	if (iary.getUsr_sms() != null) {
		bis_user.setUsr_sms(iary.getUsr_sms());
	}
	if (iary.getUsr_phs() != null) {
		bis_user.setUsr_phs(iary.getUsr_phs());
	}
	if (iary.getUsr_mail() != null) {
		bis_user.setUsr_mail(iary.getUsr_mail());
	}
	if (iary.getDept_id() != null) {
		bis_user.setDept_id_fk(iary.getDept_id());
	}
	if (iary.getValid_flg() != null) {
		bis_user.setValid_flg(iary.getValid_flg());
	}
	if (iary.getLock_flg() != null) {
		bis_user.setLock_flg(iary.getLock_flg());
	}
	if (iary.getUsr_key() != null) {
		bis_user.setUsr_key(iary.getUsr_key());
	}
	if (iary.getReg_timestamp() != null){
		String regTimeStr = iary.getReg_timestamp();
		Timestamp ts = new Timestamp(System.currentTimeMillis());
		try {
			ts = Timestamp.valueOf(regTimeStr+" 00:00:00");
			bis_user.setReg_timestamp(ts);
		}catch (Exception e){
			e.printStackTrace();
		}
	}

	bis_user.setEvt_usr(inTrx.getEvt_usr());
	bis_user.setEvt_timestamp(current_timestamp);
	bis_user.setEvt_cate(EVT_UAS_UPDATE);
	bisUserDao.saveOrUpdate(bis_user);
	
	return lRc;
}

private long addFnc(FapuasgrpI inTrx, FapuasgrpO outTrx) {
	long lRc = _NORMAL;
	Timestamp current_timestamp = Util.getCurrentTimestamp();
	FapuasgrpIA iary = inTrx.getIary().get(0);
	String usr_id = iary.getUsr_id();
	Bis_user bis_user = bisUserDao.get(usr_id);
	if (bis_user != null&&"OPI".equals(bis_user.getSystem_id())) {
		outTrx.setRtn_mesg("代码为[" + iary.getUsr_id() + "]的用户已经存在!请确认！");
		return E_BIS_USER +E_ADD_EXIST;
	}
	if(bis_user != null && "BC".equals(bis_user.getSystem_id())){//该用户已经注册BC，则更新为OAB
		bis_user.setSystem_id("OAB");
		bisUserDao.update(bis_user);
	}else{
		Bis_user bisUser = new Bis_user();
		bisUser.setUsr_id(usr_id);
		bisUser.setUsr_name(iary.getUsr_name());
		bisUser.setUsr_e_name(iary.getUsr_e_name());
		bisUser.setUsr_typ(iary.getUsr_typ());
		bisUser.setUsr_sms(iary.getUsr_sms());
		bisUser.setDept_id_fk(iary.getDept_id());
		bisUser.setUsr_phs(iary.getUsr_phs());
		bisUser.setUsr_mail(iary.getUsr_mail());
		bisUser.setValid_flg(iary.getValid_flg());
		bisUser.setLock_flg(iary.getLock_flg());
		bisUser.setUsr_key("123");
		bisUser.setSystem_id("OPI");
		String regTimeStr = iary.getReg_timestamp();
		Timestamp ts = new Timestamp(System.currentTimeMillis());
		try {
			ts = Timestamp.valueOf(regTimeStr+" 00:00:00");
			bisUser.setReg_timestamp(ts);
		}catch (Exception e){
			e.printStackTrace();
		}

		bisUser.setEvt_cate(EVT_UAS_ADD);
		bisUser.setEvt_timestamp(current_timestamp);
		bisUser.setEvt_usr(inTrx.getEvt_usr());
		bisUserDao.save(bisUser);
	}

	return lRc;
}

private long deleteFnc(FapuasgrpI inTrx, FapuasgrpO outTrx) {
	long lRc = _NORMAL;
	FapuasgrpIA iary = inTrx.getIary().get(0);
	String usr_id = iary.getUsr_id();
	Bis_user bis_user = bisUserDao.get(usr_id);
	if (bis_user == null) {
		outTrx.setRtn_mesg("not found user");
		return E_BIS_USER + E_READ_NOT_FOUND;
	}
	if("OAB".equals(bis_user.getSystem_id())){
		bis_user.setSystem_id("BC");
		bisUserDao.update(bis_user);
	}else{
		bisUserDao.delete(bis_user);
	}
	return lRc;
}

private long queryByCondition(FapuasgrpI inTrx, FapuasgrpO outTrx) {
	long lRc = _NORMAL;
	FapuasgrpIA iaryA = inTrx.getIary().get(0);
	String usr_id = iaryA.getUsr_id();
	String hql = "FROM Bis_user where 1=1 and (system_id = 'OPI'or system_id = 'OAB')  ";
	if (!Util.isSpaceCheck(usr_id)) {
		hql += "and usr_id = '"+usr_id+"'";
	}
	List<Bis_user> bis_users = bisUserDao.find(hql);
	List<FapuasgrpOA> oaryList = new ArrayList<FapuasgrpOA>();
	for (Bis_user bis_user : bis_users) {
		FapuasgrpOA oary = new FapuasgrpOA();
		oary.setUsr_id(bis_user.getUsr_id());
		oary.setUsr_name(bis_user.getUsr_name());
		oary.setUsr_e_name(bis_user.getUsr_e_name());
		oary.setUsr_typ(bis_user.getUsr_typ());
		if(bis_user.getReg_timestamp() != null) {
			oary.setReg_timestamp(bis_user.getReg_timestamp().toString());
		}
		oary.setUsr_phs(bis_user.getUsr_phs());
		oary.setUsr_sms(bis_user.getUsr_sms());
		oary.setUsr_mail(bis_user.getUsr_mail());
		oary.setUsr_head_flg(bis_user.getUsr_head_flg());
		oary.setValid_flg(bis_user.getValid_flg());
		oary.setLock_flg(bis_user.getLock_flg());
		oary.setDept_id(bis_user.getDept_id_fk());
		oaryList.add(oary);
	}
	outTrx.setOary(oaryList);
	outTrx.setTbl_cnt(oaryList.size());
	return lRc;
}

// 错误信息
public void errActionFnc(FapuasgrpO outTrx) {
    outTrx.setRtn_code("0000009");
    outTrx.setRtn_mesg("Invalid Action_flg");
}

public boolean isRootUsrFnc(String usr_id){
	boolean root_flg = false ;
	Bis_uas_usr_grpId id = new Bis_uas_usr_grpId();
	id.setGroup_id_fk(GROUP_ADMIN_FLG);
	id.setUsr_id_fk(usr_id);
	Bis_uas_usr_grp bis_uas_usr_grp = bisUasUsrGrpDao.get(id);
	if (null != bis_uas_usr_grp) {
		root_flg = true ;
	}
	return root_flg;
	}

public boolean isAuthUsrFnc(String usr_id,String func_code){
	boolean auth_flg = false;
	String hql = "From bis_uas_usr_grp where usr_id_fk = '"+usr_id+"'";
	List<Bis_uas_usr_grp> bis_uas_usr_grps = bisUasUsrGrpDao.find(hql);
	for (Bis_uas_usr_grp bis_uas_usr_grp : bis_uas_usr_grps) {
		String group_id = bis_uas_usr_grp.getId().getGroup_id_fk();
		Bis_uas_fnc_grpId bis_uas_fnc_grpId = new Bis_uas_fnc_grpId();
		bis_uas_fnc_grpId.setFunc_code_fk(func_code);
		bis_uas_fnc_grpId.setGroup_id_fk(group_id);
		Bis_uas_fnc_grp bis_uas_fnc_grp = bisUasFncGrpDao.get(bis_uas_fnc_grpId);
		if (null != bis_uas_fnc_grp) {
			auth_flg = true;
			return auth_flg;
		}
	}
	return auth_flg;
	}

	public long deleteGroup(FapuasgrpI inTrx, FapuasgrpO outTrx){
		long lRc = _NORMAL;
		String group_id = inTrx.getGroup_id();
		if(StringUtils.isBlank(group_id)){
			outTrx.setRtn_mesg("群组代码[" + group_id + "]为空，请确认！");
			return E_FAPUASGRP_DATA_EMPTY;
		}
		Bis_uas_grp bis_uas_grp = bisUasGrpDao.get(group_id);
		if(bis_uas_grp == null){
			outTrx.setRtn_mesg("群组代码[" + group_id + "]不存在，请确认！");
			return E_BIS_UAS_GRP +E_READ_NOT_FOUND;
		}
		String hql = "From Bis_uas_fnc_grp where group_id_fk='" +group_id +"'";
		List<Bis_uas_fnc_grp> bis_uas_fnc_grpList = bisUasUsrGrpDao.find(hql);
		if(bis_uas_fnc_grpList!= null && bis_uas_fnc_grpList.size()>0){
			outTrx.setRtn_mesg("群组代码[" + group_id + "]已经绑定了对应的功能代码，请先解绑后，再删除该群组代码！");
			return E_FAPUASGRP_GRP_BIND_FUNC;
		}
		bisUasGrpDao.delete(bis_uas_grp);
		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg(RETURN_MESG_OK);
		return lRc;
	}
	public long updateUasGrpFnc(FapuasgrpI inTrx, FapuasgrpO outTrx){
		long lRc = _NORMAL;
		Timestamp current_timestamp = Util.getCurrentTimestamp();
		FapuasgrpIB iaryB = inTrx.getIaryB().get(0);
		if(iaryB == null){
			outTrx.setRtn_mesg("未传群组信息，请确认！");
			return E_FAPUASGRP_DATA_EMPTY;
		}
		String group_id = inTrx.getGroup_id();
		String group_name = iaryB.getGroup_name();
		String admin_flg = iaryB.getAdmin_flg();
		String access_flg = iaryB.getAccess_flg();
		String system_id = iaryB.getSystem_id();
		if(StringUtils.isBlank(group_id)){
			outTrx.setRtn_mesg("群组代码[" + group_id + "]为空，请确认！");
			return E_FAPUASGRP_DATA_EMPTY;
		}
		if(StringUtils.isBlank(group_name)){
			outTrx.setRtn_mesg("群组名称[" + group_name + "]为空，请确认！");
			return E_FAPUASGRP_DATA_EMPTY;
		}
		Bis_uas_grp bis_uas_grp = bisUasGrpDao.get(group_id);
		if(bis_uas_grp == null){
			outTrx.setRtn_mesg("群组代码[" + group_id + "]不存在，无法修改，请确认！");
			return E_BIS_UAS_GRP +E_READ_NOT_FOUND;
		}
		//update uas group
		bis_uas_grp.setGroup_name(group_name);
		bis_uas_grp.setAdmin_flg(admin_flg);
		bis_uas_grp.setAccess_id(access_flg);
		bis_uas_grp.setSystem_id(system_id);
		bis_uas_grp.setEvt_timestamp(current_timestamp);
		bis_uas_grp.setEvt_usr(inTrx.getEvt_usr());
		bisUasGrpDao.update(bis_uas_grp);
		return lRc;
	}
}
